
//
// Maths.cpp for  in /home/goldbe_y//tek3/Projets/Maths/302graphe
// 
// Made by yoni goldberg
// Login   <goldbe_y@epitech.net>
// 
// Started on  Mon Sep 24 14:39:22 2012 yoni goldberg
// Last update Wed Oct 17 10:33:24 2012 yoni goldberg
//

#include "Maths.hpp"

Maths::Maths(const std::string &str)
{
  Parse	tmp(str);
  std::string	temp;

  _str = "";
  _sommets = tmp.getSommets();
  _tab = tmp.get_from_file();
  _line = tmp.getLine();
  _dist = new int[_sommets];
  for (int i = 0; i < _sommets; i++)
    _dist[i] = 1042;

  for (int i = 0; i < _sommets; i++)
    _str += (i + 'a');

  _p = new std::string[_sommets];
  for (int i = 0; i < _sommets; i++)
    _p[i] = "";

  _v = new int[_line];
  for (int i = 0; i <_line; i++)
    {
      temp = "";
      for (int j = 4; j < _tab[i].size(); j++)
	temp += _tab[i][j];
      _v[i] = string_to_var<int>(temp);
    }
  caca();
}

Maths::~Maths()
{
}

int	Maths::get_line()
{
  return _line;
}

int	Maths::get_sommets()
{
  return _sommets;
}

std::vector<std::string>	Maths::get_tab()
{
  return _tab;
}

int				*Maths::get_dist()
{
  return _dist;
}

std::string			*Maths::get_path()
{
  return _p;
}

void	Maths::aff_tab(std::vector<std::string> tab)
{
  for (int i = 0; i < tab.size(); i++)
    std::cout << tab[i] << std::endl;
}

void			Maths::caca()
{
  std::string		temp(_str);

  _dist[0] = 0;
  _p[0] = "";

  for (int j = 0; j < _line; j++)
    for (int i = 0; i < _line; i++)
      {
	int x = _tab[i][0];
	int y = _tab[i][2];
	if (_dist[x - 'a'] + _v[i] < _dist[y - 'a'])
	  {
	    if (j != 0)
		_p[y - 'a'] = _p[x - 'a'];
	    _dist[y - 'a'] = _dist[x - 'a'] + _v[i];
	    _p[y - 'a'] += x;
	  }
      }

  for (int i = 0; i < _sommets; i++)
    {
      if (_p[i][0] != 'a')
	_p[i].insert(0, "a");
      _p[i] += (char)(i + 'a');
    }
}
